package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Spannable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.TextView.BufferType;


import com.easemob.cases.MessageHelper;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.exceptions.HyphenateException;
import com.wechat.android.app.R;
import com.wechat.android.app.utils.MyLog;

public class EaseChatRowText extends EaseChatRow {

    private TextView contentView;
    protected Typeface typeFace = null;

    public EaseChatRowText(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
        try {
            typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/MiaoUnicodeTTF(1).ttf");
        } catch (Exception e) {

        }
    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_message : R.layout.ease_row_sent_message, this);


    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);
    }

    @Override
    public void onSetUpView() {

        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        contentView.setTypeface(typeFace);
        if (txtBody.getMessage().contains("对方不在线")) {
            //  MiaoUnicodeFFF
         contentView.setText("\uD81B\uDF3D\uD81B\uDFD3\uD81B\uDF16\uD81B\uDF54\uD81B\uDF26\uD81B\uDFA9\uD81B\uDF10\uD81B\uDFC4\uD81B\uDFBE\uD81B\uDF10\uD81B\uDFA0");

                 //shishanhmd
//            contentView.setText("\uD81B\uDF3D\uD81B\uDF7A\uD81B\uDF90\uD81B\uDF16\uD81B\uDF54\uD81B\uDF26\uD81B\uDFA9\uD81B\uDF10\uD81B\uDF61\uD81B\uDF59\uD81B\uDF90\uD81B\uDF10\uD81B\uDF54\uD81B\uDF91");
        } else {
            Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
            contentView.setText(span, BufferType.SPANNABLE);
        }

        handleTextMessage();
    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS:
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        MyLog.e("hep", message.getStringAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE, null) + "--" + message.getStringAttribute(MessageHelper.MESSAGE_EXT_DATA, null));
//        try {
//            // TODO 跳转至订单详情页inspectionOrder
//            if ("order".equals(message.getStringAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE, null))) {
//                String data = message.getStringAttribute(MessageHelper.MESSAGE_EXT_DATA, null);
//                String orderStatus = JsonUtil.getJsonValuesString(data, "orderType");
//                if ("serviceOrder".endsWith(orderStatus)) {
//                    String orderNumber = JsonUtil.getJsonValuesString(data, "orderNumber");
//                    String userOrderId = JsonUtil.getJsonValuesString(data, "userOrderId");
////                String orderStatus = JsonUtil.getJsonValuesString(data, "orderStatus");
////                String openTime = JsonUtil.getJsonValuesString(data, "openTime");
//                    getData(orderNumber, userOrderId);
//                } else if ("inspectionOrder".endsWith(orderStatus)) {
//                    String userOrderId = JsonUtil.getJsonValuesString(data, "userOrderId");
//                    String storeId = JsonUtil.getJsonValuesString(data, "storeId");
//                    context.startActivity(new Intent(context, TestingDetailsActivity.class).putExtra("storeId", storeId).putExtra("userOrderId", userOrderId));
//                }
//                // TODO 跳转至公告
//            } else if ("bulletin".equals(message.getStringAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE, null))) {
//                String data = message.getStringAttribute(MessageHelper.MESSAGE_EXT_DATA, null);
//                String infoId = JsonUtil.getJsonValuesString(data, "infoId");
//                IntentUtils.startBulletin(context, infoId);
//            }
//        } catch (Exception e) {
//
//
//            Toast.makeText(context, "参数异常", Toast.LENGTH_SHORT).show();
//        }
    }


}
